package org.feng.settingsob;

import android.app.Activity;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private static final String SAVE_NAME="save_name";
	private EditText mEditText;
	
	private SettingsValueChangeContentObserver mContentOb;
	class SettingsValueChangeContentObserver extends ContentObserver{

		public SettingsValueChangeContentObserver() {
			super(new Handler());
		}

		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
			Toast.makeText(MainActivity.this, Settings.System.getString(getContentResolver(), SAVE_NAME), Toast.LENGTH_SHORT).show();
		}
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mEditText=(EditText)findViewById(R.id.editText1);
		mContentOb=new SettingsValueChangeContentObserver();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		getContentResolver().registerContentObserver(Settings.System.getUriFor(SAVE_NAME), true, mContentOb);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		getContentResolver().unregisterContentObserver(mContentOb);
	}
	
	public void onClick(View view){
		switch (view.getId()) {
		case R.id.button1:
			String name=mEditText.getText().toString();
			Settings.System.putString(getContentResolver(), SAVE_NAME, name);
			
			break;

		default:
			break;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

}
